<?php
/*
 * Created by generator
 *
 */

class Editadherent extends Controller {

	function Editadherent(){
		parent::Controller();
		$this->load->model('adherent_model');
		$this->load->model('saison_model');
		$this->load->model('activite_model');
		$this->load->model('LienActivitesAdherents_model');
		$this->load->model('niveau_model');
		$this->load->model('trancheAge_model');
		
		$this->load->library('session');
		$this->load->helper('url');
	}


	/**
	 * Affichage des infos
	 */
	public function index($adhidadh){
		$data = $this->chargeDonnees($adhidadh);
		
		$this->load->view('editadherent_view',$data);
	}

	/**
	 * Changement des infos, mais ne sauve rien
	 */
	public function changeSaison(){
		$adhidadh = $this->input->post('adhidadh');
		$idSaison = $this->input->post('idSaison');
		$data = $this->chargeDonnees($adhidadh, $idSaison);
		
		$data['goto'] = "activites";

		$this->load->view('editadherent_view',$data);
	}
	
	/**
	 * Changement des infos, mais ne sauve rien
	 */
	public function ajouterActivite(){
		$adhidadh = $this->input->post('adhidadh');
		$idSaison = $this->input->post('idSaison');
		$actidact = $this->input->post('actidact');
		
		$model = new LienActivitesAdherents_model();
		$model->xaaidact = $actidact;
		$model->xaaidadh = $adhidadh;
		$model->save($this->db);
		
		$data = $this->chargeDonnees($adhidadh, $idSaison);
		$data['goto'] = "activites";
		
		$this->load->view('editadherent_view',$data);
	}
	
	/**
	 * Suppression du lien activité-adhérent
	 */
	public function deleteActivite() {
		$adhidadh = $this->input->post('adhidadh');
		$idSaison = $this->input->post('idSaison');
		$actidact = $this->input->post('actidact');

		$model = new LienActivitesAdherents_model();
		$model->xaaidact = $actidact;
		$model->xaaidadh = $adhidadh;
		$model->delete($this->db);
		
		$data = $this->chargeDonnees($adhidadh, $idSaison);
		$data['goto'] = "activites";
		
		$this->load->view('editadherent_view',$data);
	}
	
	
	/**
	 * Charge toutes les données communes à l'écran de mise à jour d'un adhérent 
	 * @param unknown_type $adhidadh
	 * @param unknown_type $idSaison
	 */
	private function chargeDonnees($adhidadh, $idSaison = null){
		$model = adherent_model::getadherent($this->db, $adhidadh);
		$data['adherent'] = $model;
		$data['saisons'] = saison_model::getAllsaisons($this->db);
		if( $idSaison == null ) {
			$saisonEnCours = saison_model::getCurrentSaison($this->db);
		} else {
			$saisonEnCours = saison_model::getSaison($this->db, $idSaison);
		}
		$data['saisonEnCours'] = $saisonEnCours;
		$data['activitesAdherent'] = activite_model::getActivitesForAdherentForSaison($this->db, $adhidadh, $saisonEnCours->saiidsai);
		$data['activitesSaison'] = activite_model::getActivitesForSaison($this->db, $saisonEnCours->saiidsai);
		$data['niveaux'] = niveau_model::getAllniveaus($this->db);
		$data['trancheAges'] = trancheAge_model::getAlltrancheAges($this->db);
		
		return $data;
	}
	
	/**
	 * Sauvegarde des modifications
	 */
	public function save(){
		// Mise a jour des donnees en base
		$model = new adherent_model();
		$model->adhidadh = $this->input->post('adhidadh');
		$model->adhlbnom = $this->input->post('adhlbnom'); 
		$model->adhlbprn = $this->input->post('adhlbprn'); 
		$model->adhtxadr = $this->input->post('adhtxadr'); 
		$model->adhlbtel = $this->input->post('adhlbtel'); 
		$model->adhlbmai = $this->input->post('adhlbmai'); 
		$model->update($this->db);
		
		// Upload du fichier photo
		$config['upload_path'] = realpath('www/photosAdherents/');
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
		$config['max_size']	= '0';
		$config['max_width']  = '0';
		$config['max_height']  = '0';
		$this->load->library('upload', $config);
		$path = $config['upload_path'] . "/";
		$thumbnailsPath = $path . "thumbnails" . "/";
		
		// suppression de l'ancienne photo
		if( $this->input->post('checkSuppPhoto') == "on" ) {
			$newModel = adherent_model::getadherent($this->db, $model->adhidadh);
			unlink($path . $newModel->adhlbpho);
			unlink($thumbnailsPath . $newModel->adhlbpho);
			$newModel->adhlbpho = null;
			$newModel->updatePhoto($this->db);
		}
		
		$uploadDataImage = null;
		if($this->upload->do_upload('adhlbpho_image')){
			$uploadDataImage = $this->upload->data('adhlbpho_image');
			//$this->session->set_flashdata('message', 'photo récupérée:' . $uploadDataImage);
			
		} else {
			// pas de photo à charger
			//$this->session->set_flashdata('message', $this->upload->display_errors() );
		}
		if($uploadDataImage != null) {
			$model->adhlbpho = 'adh_' . $model->adhidadh . '_img' . $uploadDataImage['file_ext'];
			//rename($path . $uploadDataImage['file_name'], $path . $model->adhlbpho);
			// creation de la grande image
			adherent_model::createThumbnail($path, $uploadDataImage['file_name'], $path, $model->adhlbpho, 100, 150);
			// creation de la petite image
			adherent_model::createThumbnail($path, $uploadDataImage['file_name'], $thumbnailsPath, $model->adhlbpho, 20, 20);
			// suppression de l'image téléchargée
			unlink($path . $uploadDataImage['file_name']);
			$model->updatePhoto($this->db);
		}
				
		$this->session->set_flashdata('message', 'adherent mis a jour');

		redirect('listadherents/index'); 
	}
	
}
?>
